<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="数据库" prop="tableSchema">
        <el-select
          v-model="queryParams.tableSchema"
          placeholder="请选择数据库"
          clearable
          @change="getList"
          filterable
        >
          <el-option
            v-for="item in tableSchemaOptions"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="表名称" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表描述" prop="tableComment">
        <el-input
          v-model="queryParams.tableComment"
          placeholder="请输入表描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          @click="handleExportTable"
          v-hasPermi="['tool:db-doc:export']"
          >导出表结构</el-button
        >
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          @click="handleOperateDoc"-->
<!--          v-hasPermi="['tool:operate-doc:generate']"-->
<!--          >功能文档生成</el-button-->
<!--        >-->
      </el-col>
    </el-row>
    <el-row>
      <el-table
        ref="table"
        v-loading="loading" 
        :data="dbTableList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column
          prop="tableName"
          label="表名称"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="tableComment"
          label="表描述"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              icon="View"
              @click="handleInfo(scope.row)"
              v-hasPermi="['tool:db-doc:info']"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <infoDrawer ref="infoDrawerRef" />
    <operateDocDialog ref="operateDocDialogRef" />
  </div>
</template>

<script setup>
import { listDbTable, allTableSchema } from "@/api/tool/db-doc";
import infoDrawer from "./infoDrawer.vue";
import operateDocDialog from "./operateDocDialog.vue";
const { proxy } = getCurrentInstance();
const total = ref(0);
const visible = ref(false);
const loading = ref(false);
const tables = ref([]);
const tableSchemaOptions = ref([]);
const dbTableList = ref([]);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  tableName: undefined,
  tableComment: undefined,
});

const emit = defineEmits(["ok"]);

/** 查询参数列表 */
function show() {
  getList();
  visible.value = true;
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  tables.value = selection.map((item) => item.tableName);
}
/** 查询表数据 */
function getList() {
  loading.value = true
  listDbTable(queryParams).then((res) => {
    dbTableList.value = res.data.list;
    total.value = res.data.total;
  }).finally(() =>  loading.value = false);
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

async function handleExportTable() {
  proxy.download(
    "/system/db-doc/export",
    { tableNames: tables.value, tableSchema: queryParams.tableSchema },
    `表结构信息${new Date().getTime()}.docx`
  );
}

async function handleInfo(row) {
  proxy.$refs.infoDrawerRef.open(true,queryParams.tableSchema, row.tableName, row.tableComment);
}
const handleOperateDoc = () => {
  proxy.$refs.operateDocDialogRef.open(true);
}

const getTableSchemaOptions = async () => {
 const res = await allTableSchema()
 tableSchemaOptions.value = res.data
}

getTableSchemaOptions()
getList();
</script>
